package com.xaicode.template.batch.decider;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.time.LocalDate;

/**
 * @author beborn
 * @date 2021/12/30 0030 16:57
 */
@Component
public class MyDecider implements JobExecutionDecider {

    public final static String WEEKEND = "weekend";
    public final static String WORKING_DAY = "workingDay";

    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        LocalDate now = LocalDate.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();

        return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY ?
                new FlowExecutionStatus(WEEKEND) : new FlowExecutionStatus(WORKING_DAY);
    }
}
